package com.hskn.hss.module.tltreespec.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tltreespec.entity.TlTreespec;
import com.hskn.hss.module.tltreespec.mapper.TlTreespecMapper;
import com.hskn.hss.module.tltreespec.service.ITlTreespecService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.DataMap;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-14
 */
@Service
public class TlTreespecServiceImpl extends ServiceImpl<TlTreespecMapper, TlTreespec> implements ITlTreespecService {
    @Resource
    TlTreespecMapper tlTreespecMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(TlTreespec tlTreespec) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlTreespec.setCreateby(uid);
        tlTreespec.setCreatetime(new Date());
        tlTreespec.setDeptid(deptid);
        if (tlTreespec.getStatus() == null || tlTreespec.getStatus().isEmpty()) {
            tlTreespec.setStatus("1");
        }
        int i = tlTreespecMapper.insert(tlTreespec);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(TlTreespec tlTreespec) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        tlTreespec.setUpdateby(uid);
        tlTreespec.setUpdatetime(new Date());
        tlTreespecMapper.updateById(tlTreespec);
        return AjaxResult.success("操作成功");
    }
    @Override
    public List<DataMap> treespecTree(){
        return tlTreespecMapper.treespecTree();
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlTreespecMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlTreespec getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlTreespec tlTreespec = tlTreespecMapper.selectById(id);
        return tlTreespec;
    }

    @Override
    public List getSelectList() {
        List resultList = tlTreespecMapper.getSelectList();
        return resultList;
    }

    @Override
    @DataScope(deptAlias = "deptid", userAlias = "createby, updateby")
    public JSONObject getList(BaseEntity entity, JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("treespecname", params.getString("treespecname"));
        map.put("status", params.getString("status"));
        map.put("params", entity.getParams());

        Page<TlTreespec> listPage = new Page<>(page, size, true);
        IPage<TlTreespec> iPage = tlTreespecMapper.listTlTreespec(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("treespeclist", iPage.getRecords());
        return json;
    }
}
